#!/usr/bin/python

import sys
import os
import re

topic_re = re.compile(r'<h2>.*span class="mw-headline"[^>]*>([^[<]*)<')
subtopic_re = re.compile(r'<h3>.*span class="mw-headline"[^>]*>([^[<]*)<')
conf_re = re.compile(r'<li>([^<-]*)[-<][^a]*a href="([^&]*)&[^>]*>([^<]*)<')

CS = "COMPUTER_SCIENCE_PROCEEDINGS"
URL_PREFIX = "http://en.wikipedia.org/"

# Parse topics and subtopics as primitive concepts.
# All topics are subsumed by COMPUTER_SCIENCE_PROCEEDINGS
def main(wikifile):
    topic = ""
    subtopic = ""

    for line in open(wikifile):
	mo = topic_re.search(line)
	if not mo == None:
	    topic = mo.group(1).strip().replace(" ", "_")
	    subtopic = topic #in case we don't have a subtopic
	    print "(implies", topic, CS + ")"

	mo = subtopic_re.search(line)
	if not mo == None:
	    subtopic = mo.group(1).strip().replace(" ", "_")
	    print "(implies", topic, CS + ")"


	mo = conf_re.search(line)
	if not mo == None:
	    abbrv = mo.group(1).strip().replace(" ", "_")
	    url = mo.group(2).strip().replace(" ", "_")
	    name = mo.group(3).strip().replace(" ", "_")

	    if (abbrv == ""):
		abbrv = name
		if (abbrv.endswith("Conference")):
		    abbrv = abbrv[ : -10] 
	    
	    print "(implies %s (and (and (and %s (= (role abbreviation) %s)) (= (role url) %s)) (= (role name) %s)))" % (abbrv, subtopic, abbrv, URL_PREFIX + url, name)


if __name__=="__main__":
    main(sys.argv[1])
